/**
 * Application.java
 */
package com.mht.cfa.server.config;

import com.mht.cfa.server.consts.ApplicationConsts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author xyt
 */
public class ApplicationStarter {

	private static Logger LOGGER = LoggerFactory.getLogger(ApplicationStarter.class);
	private static final String actionName = "应用加载器";

	public static void start() {
		ConfigLoader.init();
		override();
		LOGGER.info(String.format("%s-应用流程初始化完毕", actionName));
	}

	private static void override() {
		overridePort();
	}

	private static void overridePort() {
		String property = System.getProperty(ApplicationConsts.SYSTEM_SERVER_PORT_KEY);
		if (null != property) {
			try {
				LOGGER.info(String.format("%s-将Restful的端口覆盖为: '%s'(来自jvm参数覆盖)", actionName, property));
				Config.springBootPort = Integer.parseInt(property);
			} catch (Exception e) {
				doOverridePort();
			}
		} else {
			doOverridePort();
		}
	}

	private static void doOverridePort() {
		System.setProperty(ApplicationConsts.SYSTEM_SERVER_PORT_KEY, String.valueOf(Config.springBootPort));
	}
}
